package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.GoodsExample;
import com.abaaba.project2.bean.User;
import com.abaaba.project2.bean.UserExample;
import com.abaaba.project2.bean.vo.AdminOrderStatisticsVO;
import com.abaaba.project2.bean.vo.AdminUserStatisticsVO;
import com.abaaba.project2.bean.vo.UserInfoVo;
import com.abaaba.project2.mapper.UserMapper;
import com.abaaba.project2.service.AdminUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserInfoVo getUserList(String username, String mobile, Integer page, Integer limit, String sort, String order) {
        // 执行查询之前进行分页
        PageHelper.startPage(page, limit);

        // 构造 userExample 对象
        UserExample userExample = new UserExample();

        // 排序方式
        userExample.setOrderByClause(sort + " " + order);

        // 构造查询条件
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        // 传入 userExample 对象查询
        List<User> users = userMapper.selectByExample(userExample);

        // 获得分页信息
        PageInfo<User> userPageInfo = new PageInfo<>(users);

        long total = userPageInfo.getTotal();
        // 这是前端当前页的数据量
//        int total = users.size();

        UserInfoVo userInfoVO = new UserInfoVo();
        userInfoVO.setTotal((int) total);
        userInfoVO.setItems(users);

        return userInfoVO;
    }

    @Override
    public AdminUserStatisticsVO userNumberSearchAllCount() {
        // 构造 userExample 对象
        UserExample userExample = new UserExample();
        Long count = userMapper.countByExample(userExample);
        AdminUserStatisticsVO adminUserStatisticsVO = new AdminUserStatisticsVO();
        AdminUserStatisticsVO.DataBean dataBean = new AdminUserStatisticsVO.DataBean();
        List<String> objects1 = new ArrayList<>();
        objects1.add("day");
        objects1.add("users");
        dataBean.setColumns(objects1);

        List<AdminUserStatisticsVO.DataBean.RowsBean> objects = new ArrayList<>();
        AdminUserStatisticsVO.DataBean.RowsBean rowsBean = new AdminUserStatisticsVO.DataBean.RowsBean(count);
        objects.add(rowsBean);

        dataBean.setRows(objects);

        adminUserStatisticsVO.setData(dataBean);
        adminUserStatisticsVO.setErrmsg("成功");
        adminUserStatisticsVO.setErrno(0);

        return adminUserStatisticsVO;
    }

    @Override
    public Long countSearchUserAll() {
        UserExample userExample = new UserExample();
        // 构造查询条件
        UserExample.Criteria criteria = userExample.createCriteria();
        Long code = userMapper.countByExample(userExample);
        return code;
    }

}
